var _api = _interopRequireDefault(require("../../../../utils/api")), _util2 = _interopRequireDefault(require("../../../../utils/util")), _util3 = _interopRequireDefault(require("../../../../public/utils/util"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _objectSpread(e) {
    for (var t = 1; t < arguments.length; t++) {
        var i = null != arguments[t] ? arguments[t] : {}, r = Object.keys(i);
        "function" == typeof Object.getOwnPropertySymbols && (r = r.concat(Object.getOwnPropertySymbols(i).filter(function(t) {
            return Object.getOwnPropertyDescriptor(i, t).enumerable;
        }))), r.forEach(function(t) {
            _defineProperty(e, t, i[t]);
        });
    }
    return e;
}

function _defineProperty(t, e, i) {
    return e in t ? Object.defineProperty(t, e, {
        value: i,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = i, t;
}

var LIMIT = 20, JUSTNOW = "刚刚查看的名片";

Component({
    attached: function() {
        this.offset = 0;
    },
    data: {
        loading: !0,
        groupedList: {},
        groupedKeys: [],
        guideShown: !1,
        showSkeleton: !0,
        viewCardData: {
            groupedKeys: [ JUSTNOW ],
            groupedList: _defineProperty({}, JUSTNOW, [])
        },
        JUSTNOW: JUSTNOW
    },
    ready: function() {
        this.onLoad();
    },
    methods: {
        onLoad: function() {
            var i = this;
            this.setData({
                groupedKeys: [ "###" ],
                groupedList: {
                    "###": Array(10).fill({
                        realname: "****",
                        corpname: "****"
                    })
                }
            }, function() {
                return i.selectComponent("#skeleton").create(i);
            }), this.loadHistory(!0), this.$addListener("card.view.back", function(t) {
                var e = i.data.viewCardData;
                e.groupedList[JUSTNOW].push(t), i.setData({
                    viewCardData: e
                });
            });
        },
        onPullDownRefresh: function() {
            this.loadHistory(!0, wx.stopPullDownRefresh);
        },
        onReachBottom: function() {
            var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : function() {};
            if (this.nomore) return t();
            this.loadHistory(!1, t);
        },
        loadHistory: function(e, t) {
            var i = this;
            e && (this.offset = 0), _api.default.weappcard.history({
                data: {
                    limit: LIMIT,
                    offset: this.offset,
                    isqyy: 0,
                    sortby: "addtime",
                    orderby: "desc",
                    keyword: this.keyword || ""
                },
                complete: t
            }).then(function(t) {
                return i.handleList(t, e);
            });
        },
        handleList: function(t, e) {
            var i = t.data, r = _util3.default.groupByTimestampDesc(i, "updatetime");
            if (e) this.setData(_objectSpread({
                loading: !1,
                showSkeleton: !1
            }, r)); else {
                var o = _util2.default.concatListToData(r, this.data);
                this.setData(o);
            }
            this.offset += LIMIT, this.nomore = i.length < LIMIT;
        },
        onSearch: function(t) {
            this.keyword = t, this.loadHistory(!0);
        },
        tiggerSearch: function(t) {
            !t && this.keyword && (this.keyword = "", this.loadHistory(!0)), this.setData({
                inputShow: t
            });
        },
        goView: function(t) {
            var e = t.currentTarget.dataset.card, i = e.aid, r = e.cardid, o = e.guid;
            this.$route("/pages/card/view/view", {
                aid: i,
                cardid: r,
                guid: o
            });
        }
    }
});